package com.easybenefit.UUService.fragment;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUStrUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.model.MerchantsInfoBean;
import com.easybenefit.UUService.myinterface.IActivityInterface;


public class ContactFragment extends Fragment implements IActivityInterface, View.OnClickListener {

    private Context context;

    private View contentView;
    private LinearLayout llt_address;
    private LinearLayout llt_phone;
    private LinearLayout llt_tel;
    private TextView tv_address;
    private TextView tv_phone;
    private TextView tv_tel;
    private MerchantsInfoBean merchantsInfoBean;

    public static ContactFragment newInstance() {
        ContactFragment fragment = new ContactFragment();
        fragment.setRetainInstance(true);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        contentView = inflater.inflate(R.layout.fragment_contact, container, false);
        initView();
        initData();
        initListener();
        return contentView;
    }


    @Override
    public void initData() {

        tv_phone.setText(tv_phone.getText().toString() + (merchantsInfoBean.getMer_phone() == null ? "" : merchantsInfoBean.getMer_phone()));
        tv_address.setText(tv_address.getText().toString() + (merchantsInfoBean.getMer_addr() == null ? "" : merchantsInfoBean.getMer_addr()));
        tv_tel.setText(tv_tel.getText().toString() + (merchantsInfoBean.getMer_tel() == null ? "" : merchantsInfoBean.getMer_tel()));
    }

    @Override
    public void initView() {
        llt_address = (LinearLayout) contentView.findViewById(R.id.llt_address);
        llt_phone = (LinearLayout) contentView.findViewById(R.id.llt_phone);
        llt_tel = (LinearLayout) contentView.findViewById(R.id.llt_tel);
        tv_address = (TextView) contentView.findViewById(R.id.tv_address);
        tv_tel = (TextView) contentView.findViewById(R.id.tv_tel);
        tv_phone = (TextView) contentView.findViewById(R.id.tv_phone);
    }

    @Override
    public void initListener() {
        llt_address.setOnClickListener(this);
        llt_phone.setOnClickListener(this);
        llt_tel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llt_address:
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                MyMapFragment myMapFragment= new MyMapFragment().newInstance();
                Bundle bundle=new Bundle();
                if (merchantsInfoBean.getMer_location()!=null&&merchantsInfoBean.getMer_location()!=""){
                    try {
                        String[] result = merchantsInfoBean.getMer_location().split(",");
                        bundle.putDouble("longitude", Double.valueOf(result[0]));
                        bundle.putDouble("latitude", Double.valueOf(result[1]));
                        bundle.putString("address",merchantsInfoBean.getMer_addr());
                        myMapFragment.setArguments(bundle);
                        transaction.replace(R.id.container, myMapFragment).commit();
                    }catch (Exception e){
                        e.printStackTrace();
                        UUToastUtil.showToast(context,"该商户位置信息有误");
                    }
                }else {
                    UUToastUtil.showToast(context,"该商户没有上报位置信息");
                }

                break;
            case R.id.llt_phone:
                if (!UUStrUtil.isEmpty(merchantsInfoBean.getMer_phone())) {
                    Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri
                            .parse("tel:" + merchantsInfoBean.getMer_phone()));
                    startActivity(callIntent);
                }
                break;
            case R.id.llt_tel:
                if (!UUStrUtil.isEmpty(merchantsInfoBean.getMer_tel())) {
                    Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri
                            .parse("tel:" + merchantsInfoBean.getMer_tel()));
                    startActivity(callIntent);
                }
                break;
        }
    }

    public MerchantsInfoBean getMerchantsInfoBean() {
        return merchantsInfoBean;
    }

    public void setMerchantsInfoBean(MerchantsInfoBean merchantsInfoBean) {
        this.merchantsInfoBean = merchantsInfoBean;
    }
}
